# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4 import QtCore, QtGui
from ui_login import Ui_Login
from ui_password import Ui_Password

import sys
sys.path.append('../')

import database.userUtils
from MySQLdb import OperationalError

class LoginDialog(QtGui.QDialog):

	def __init__(self, controller):
		QtGui.QDialog.__init__(self)

		self.controller = controller

		# Set up the user interface from Designer.
		self.ui = Ui_Login()
		self.ui.setupUi(self)

		self.settings = QtCore.QSettings("GaTech", "OriginDB")
		host = self.settings.value("db/hostname", QtCore.QVariant("localhost")).toString()
		dbName = self.settings.value("db/dbname", QtCore.QVariant("chemdb")).toString()

		self.ui.databaseHostLineEdit.setText(host)
		self.ui.databaseNameLineEdit.setText(dbName)

		self.ui.usernameEdit.hide()
		self.ui.passwordEdit.hide()
		self.ui.usernameLabel.hide()
		self.ui.passwordLabel.hide()

		self.connect(self.ui.loginButton, QtCore.SIGNAL("clicked()"), self.loginAction)
		self.connect(self.ui.writeCheckBox, QtCore.SIGNAL("clicked()"), self.writeCheck)
		
		
	def loginAction(self):

		if self.ui.writeCheckBox.isChecked():
			username = str(self.ui.usernameEdit.text())
			password = str(self.ui.passwordEdit.text())
		else:
			username = ''
			password = ''

		host = str(self.ui.databaseHostLineEdit.text())
		db = str(self.ui.databaseNameLineEdit.text())
		
		try:
			database.userUtils.login(db, host, '', '')
			database.userUtils.login(db, host, username, password)
		except ValueError:
			self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Critical, "Error", "Wrong username or password!", QtGui.QMessageBox.Ok)
			self.dialog.setParent(self.parentWidget())
			self.dialog.setModal(True)
			self.dialog.show()
			return
		except OperationalError:
			self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Critical, "Error", "Error connecting to the database", QtGui.QMessageBox.Ok)
			self.dialog.setParent(self.parentWidget())
			self.dialog.setModal(True)
			self.dialog.show()
			return
		
		self.controller.setReadonly()

		self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Information, "Logged in", "Logged in", QtGui.QMessageBox.Ok)
		self.dialog.setParent(self.parentWidget())
		self.dialog.setModal(True)
		self.dialog.show()

		self.settings.setValue("db/hostname", QtCore.QVariant(host))
		self.settings.setValue("db/dbname", QtCore.QVariant(db))

		self.close()

	def writeCheck(self):
		if self.ui.writeCheckBox.isChecked():
			self.ui.usernameEdit.show()
			self.ui.passwordEdit.show()
			self.ui.usernameLabel.show()
			self.ui.passwordLabel.show()
		else:
			self.ui.usernameEdit.hide()
			self.ui.passwordEdit.hide()
			self.ui.usernameLabel.hide()
			self.ui.passwordLabel.hide()


class PasswordDialog(QtGui.QDialog):

	def __init__(self):
		QtGui.QDialog.__init__(self)

		# Set up the user interface from Designer.
		self.ui = Ui_Password()
		self.ui.setupUi(self)

		self.connect(self.ui.changeButton, QtCore.SIGNAL("clicked()"), self.changePass)

	def changePass(self):

		oldPass = str(self.ui.passwordLineEdit.text())
		newPass = str(self.ui.newLineEdit.text())
		confirmPass = str(self.ui.confirmLineEdit.text())

		if len(newPass) < 4:
			self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Critical, "Error", "Password is too short.\nMust be more than 4 characters.", QtGui.QMessageBox.Ok)
			self.dialog.setParent(self.parentWidget())
			self.dialog.setModal(True)
			self.dialog.show()

		if newPass != confirmPass:
			self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Critical, "Error", "New usernames do not match", QtGui.QMessageBox.Ok)
			self.dialog.setParent(self.parentWidget())
			self.dialog.setModal(True)
			self.dialog.show()
			return

		try:
			database.userUtils.changePass(oldPass, newPass)
		except ValueError:
			self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Critical, "Error", "Old password does not match", QtGui.QMessageBox.Ok)
			self.dialog.setParent(self.parentWidget())
			self.dialog.setModal(True)
			self.dialog.show()
			return

		self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Information, "Success", "Password changed", QtGui.QMessageBox.Ok)
		self.dialog.setParent(self.parentWidget())
		self.dialog.setModal(True)
		self.dialog.show()

		self.close()

